package com.pine.sunshineelder.checkin.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pine.sunshineelder.checkin.ao.relative.relativeAddAo;
import com.pine.sunshineelder.checkin.ao.relative.relativeModAo;
import com.pine.sunshineelder.checkin.service.RelativeService;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dao.ElderDao;
import com.pine.sunshineelder.common.dao.RelativeDao;
import com.pine.sunshineelder.common.dao.RelativeElderDao;
import com.pine.sunshineelder.common.entity.ElderEntity;
import com.pine.sunshineelder.common.entity.RelativeElderEntity;
import com.pine.sunshineelder.common.entity.RelativeEntity;
import com.pine.sunshineelder.common.exception.ThisSystemException;
import com.pine.sunshineelder.common.validator.ValidateHelper;

import lombok.NonNull;

@Service
public class RelativeServiceImpl implements RelativeService, StringHelper {
	@Autowired
	ElderDao edao;
	@Autowired
	RelativeDao rdao;
	@Autowired
	RelativeElderDao redao;

	@Override
	public RelativeEntity updateRelative(String elderId,@NonNull relativeAddAo ao) throws Exception {
		// 清理参数
		elderId = ValidateHelper.$("老人id", elderId);
		String name = trimAsNull(ao.getName());
		String idCard = trimAsNull(ao.getIdCard());
		String relation = trimAsNull(ao.getRelation());
		String workPhone = trimAsNull(ao.getWorkPhone());
		String privatePhone = trimAsNull(ao.getPrivatePhone());
		String email = trimAsNull(ao.getEmail());
		String address = trimAsNull(ao.getAddress());
		String wechat = trimAsNull(ao.getWechat());
		// 业务逻辑
		// 判断id是否存在
		ElderEntity elder = edao.selectById(elderId);
		if (elder == null) {
			throw new ThisSystemException("id.not-exisits", "非法id");
		}
		RelativeEntity rel = new RelativeEntity();
		rel.setElderId(elderId);
		rel.setAddress(address);
		rel.setId(uuid());
		rel.setEmail(email);
		rel.setIdCard(idCard);
		rel.setName(name);
		rel.setPrivatePhone(privatePhone);
		rel.setRelation(relation);
		rel.setMale(ao.isMale());
		rdao.insert(rel);
		RelativeElderEntity re = new RelativeElderEntity();
		re.setElderId(elderId);
		re.setRelativeId(rel.getId());
		redao.insert(re);
		return rel;
	}

	@Override
	public RelativeEntity modifyRelative(String id, relativeModAo ao) throws Exception {
		id = ValidateHelper.$("id", id);

		// 判断id是否存在
		RelativeEntity rel = rdao.selectById(id);
		if (rel == null) {
			throw new ThisSystemException("id.not-exisits", "非法id");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		String workPhone = trimAsNull(ao.getWorkPhone());
		if (workPhone != null) {
			if (!Objects.equals(rel.getWorkPhone(), workPhone)) {
				rel.setWorkPhone(workPhone);
				map.put("workPhone", workPhone);
			}
		}
		String privatePhone = trimAsNull(ao.getPrivatePhone());
		if (privatePhone != null) {
			if (!Objects.equals(rel.getPrivatePhone(), privatePhone)) {
				rel.setPrivatePhone(privatePhone);
				map.put("privatePhone", privatePhone);
			}
		}
		String email = trimAsNull(ao.getEmail());
		if (email != null) {
			if (!Objects.equals(rel.getEmail(), email)) {
				rel.setEmail(email);
				map.put("email", email);
			}
		}
		String address = trimAsNull(ao.getAddress());
		if (address != null) {
			if (!Objects.equals(rel.getAddress(), address)) {
				rel.setAddress(address);
				map.put("address", address);
			}
		}
		String wechat = trimAsNull(ao.getWechat());
		if (wechat != null) {
			if (!Objects.equals(rel.getWechat(), wechat)) {
				rel.setWechat(wechat);
				map.put("wechat", wechat);
			}
		}
		if (ao.getName() != null) {
			if (!Objects.equals(rel.getName(), ao.getName())) {
				rel.setName(ao.getName());
				;
				map.put("name", ao.getName());
			}
		}
		if (ao.getRelation() != null) {
			if (!Objects.equals(rel.getRelation(), ao.getRelation())) {
				rel.setRelation(ao.getRelation());
				;
				map.put("relation", ao.getRelation());
			}
		}
		if (ao.getIdCard() != null) {
			if (!Objects.equals(rel.getIdCard(), ao.getIdCard())) {
				rel.setIdCard(ao.getIdCard());
				;
				map.put("idCard", ao.getIdCard());
			}
		}
		if (!map.isEmpty()) {
			rdao.update(id, map);
		}
		return rel;
	}

	@Override
	public RelativeEntity queryById(@NotBlank String id) throws Exception {
		RelativeEntity rel = rdao.selectById(id);
		if (rel == null) {
			throw new ThisSystemException("id.not-exisits", "非法id");
		}
		return rel;
	}

}
